package com.hupu.jrs.ui.messagelist

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.hupu.jrs.R
import com.hupu.jrs.bean.Message
import java.util.*
import javax.inject.Inject

/**
 * Created by sll on 2016/3/11.
 */
class MessageListAdapter @Inject
constructor() : RecyclerView.Adapter<MessageListAdapter.ViewHolder>() {

    private var messages: MutableList<Message> = ArrayList<Message>()
    private var onItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
        this.onItemClickListener = onItemClickListener
    }

    interface OnItemClickListener {
        fun onMessageClick(message: Message?)
    }

    fun bind(messages: MutableList<Message>) {
        this.messages = messages
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context)
                .inflate(R.layout.item_list_message, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val message = messages[position]
        holder.message = message
        holder.tvTime!!.setText(message.time)
        holder.tvCategory!!.setText(message.catergory)
        holder.tvInfo!!.setText(message.info)
    }

    override fun getItemCount(): Int {
        return messages.size
    }

    fun remove(message: Message) {
        messages.remove(message)
        notifyDataSetChanged()
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        @BindView(R.id.tvCategory)
        var tvCategory: TextView? = null
        @BindView(R.id.tvInfo)
        var tvInfo: TextView? = null
        @BindView(R.id.tvTime)
        var tvTime: TextView? = null

        var message: Message? = null

        @OnClick(R.id.listItem)
        fun listItemClick() {
            if (onItemClickListener != null) {
                onItemClickListener!!.onMessageClick(message)
            }
        }

        init {
            ButterKnife.bind(this, itemView)
        }
    }
}
